package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.domain.Order;
import com.example.demo.domain.Room;
import com.example.demo.domain.Type;
import com.example.demo.dto.AdminReturnRoomDTO;
import com.example.demo.dto.DateSectionDTO;
import com.example.demo.dto.ReturnRoomDTO;
import com.example.demo.mapper.OrderMapper;
import com.example.demo.mapper.RoomMapper;
import com.example.demo.mapper.TypeMapper;
import com.example.demo.service.RoomService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

 /**
 * @author liuchao
 * @version 1.0
 * @date 2024/11/7
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private TypeMapper typeMapper;

    @Override
    public Boolean bookRoom(Integer roomId) {
        return this.getBaseMapper().bookRoom(roomId);
    }

    @Override
    public Boolean finishRoom(Integer roomId) {
        return this.getBaseMapper().finishRoom(roomId);
    }

    @Override
    public List<ReturnRoomDTO> listRooms(DateSectionDTO dateSectionDTO) {
        QueryWrapper<Room> roomQueryWrapper = new QueryWrapper<>();

        // 查询空闲房间
        List<Room> roomList = this.baseMapper.selectList(roomQueryWrapper.eq("state", 0));
        Map<Integer, Room> roomMap = roomList.stream().collect(Collectors.toMap(Room::getId, a -> a,(k1, k2)->k1));

        /**
         * lt：less than 小于
         * gt：greater than 大于
         */
        // 根据传入时间来筛选对应的订单
        // 该订单是否被处理  0：未处理  1：办理入住  2：退订  3：订单已完成且过期
//        List<Order> orders = orderMapper.selectList(new QueryWrapper<Order>()
//                .eq("flag", 1)
//                .lt("in_time", dateSectionDTO.getInTime())
//                // 判断leave_time是否小于in_time,lt是小于
//                .lt("in_time", dateSectionDTO.getLeaveTime())
//                // 判断leave_time是否大于in_time,gt是大于
//                .gt("leave_time", dateSectionDTO.getInTime())
//                // 判断leave_time是否小于leave_time
//                .lt("leave_time", dateSectionDTO.getLeaveTime())
//                .or()
//                .eq("flag", 1)
//                .gt("in_time", dateSectionDTO.getInTime())
//                .lt("in_time", dateSectionDTO.getLeaveTime())
//                .gt("leave_time", dateSectionDTO.getInTime())
//                .gt("leave_time", dateSectionDTO.getLeaveTime())
//                .or()
//                .eq("flag", 1)
//                .gt("in_time", dateSectionDTO.getInTime())
//                .lt("in_time", dateSectionDTO.getLeaveTime())
//                .gt("leave_time", dateSectionDTO.getInTime())
//                .lt("leave_time", dateSectionDTO.getLeaveTime())
//                .or()
//                .eq("flag", 1)
//                .lt("in_time", dateSectionDTO.getInTime())
//                .lt("in_time", dateSectionDTO.getLeaveTime())
//                .gt("leave_time", dateSectionDTO.getInTime())
//                .gt("leave_time", dateSectionDTO.getLeaveTime()));

        // 修改判断的逻辑，首先找到flag不为2的，也就是单子不是退订的那一批，
        // 这一批单子都是符合条件的要筛选掉的房子
        List<Order> orders = orderMapper.selectList(new QueryWrapper<Order>()
                .ne("flag", 2)
                // leave_time>12.3 12点
                .gt("leave_time", dateSectionDTO.getInTime())
               );


        // 假如查询到这些订单，就通过订单来筛选掉对应的房间
        for (Order order : orders) {
            roomMap.remove(order.getRoomId());
        }
        List<Room> rooms = new ArrayList<>(roomMap.values());

        List<ReturnRoomDTO> returnRoomDTOList = new ArrayList<>();

        // 把拿到大rooms进行修改，封装，然后返回数据
        if (rooms.size() != 0) {
            for (Room room : rooms) {
                ReturnRoomDTO returnRoomDTO = split(room);
                BeanUtils.copyProperties(room, returnRoomDTO);
                Type type = typeMapper.selectById(room.getType());
                returnRoomDTO.setType(type);
                returnRoomDTOList.add(returnRoomDTO);
            }
        }

        return returnRoomDTOList;
    }

    @Override
    public ReturnRoomDTO roomDetail(Integer roomId) {
        Room room = this.getBaseMapper().selectById(roomId);
        ReturnRoomDTO returnRoomDTO = split(room);
        Type type = typeMapper.selectById(room.getType());
        // System.out.println(type);
        returnRoomDTO.setType(type);
        return returnRoomDTO;
    }

    @Override
    public AdminReturnRoomDTO adminRoomDetail(Integer roomId) {
        Room room = this.getBaseMapper().selectById(roomId);

        AdminReturnRoomDTO roomDTO = new AdminReturnRoomDTO();
        roomDTO.setRoom(room);

        Type type = typeMapper.selectById(room.getType());
        roomDTO.setType(type);

        return roomDTO;
    }

    private ReturnRoomDTO split(Room room) {
        ReturnRoomDTO returnRoomDTO = new ReturnRoomDTO();
        BeanUtils.copyProperties(room, returnRoomDTO);

        Map<String, String> introduces = new HashMap<>();
        String[] items = room.getIntroduce().split(",");
        for(String str : items) {
            String[] strs = str.split(":");
            introduces.put(strs[0], strs[1]);
        }

        returnRoomDTO.setIntroduces(introduces);

        return returnRoomDTO;
    }
}

